package com.john.bms.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.john.bms.mapper.system.DictionariesMapper;
import com.john.bms.model.system.dictionaries.DictionariesPage;
import com.john.bms.model.system.dictionaries.SysDictionarie;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author:张家小帅哥
 * @Date:2020/11/30 11:28
 * @Version:1.0
 * @Description:
 */
@Service
public class DictionariesService {

    @Resource
    private DictionariesMapper dictionariesMapper;

    public PageInfo<SysDictionarie> getPageList(DictionariesPage page) {

        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysDictionarie> sysDictionaries = dictionariesMapper.selectPageList(page);
        PageInfo<SysDictionarie> pageInfo = new PageInfo<>(sysDictionaries);
        return pageInfo;
    }

    public boolean addDictionaries(SysDictionarie de) {
        de.setPatentId(0);
        de.setCreateTime(new Date());

        int insert = dictionariesMapper.insert(de);

        return insert > 0 ? true : false;
    }

    public SysDictionarie getDictionariesInfo(String id) {

        return dictionariesMapper.selectById(id);
    }

    public boolean updateDictionaries(SysDictionarie de) {
        de.setUpdateTime(new Date());
        int result = dictionariesMapper.updateById(de);
        return result > 0 ? true : false;
    }

    public boolean deleteDictionaries(String id) {
        QueryWrapper<SysDictionarie> wrapper = new QueryWrapper<>();
        wrapper.eq("patent_id", id);
        List<SysDictionarie> sysDictionaries = dictionariesMapper.selectList(wrapper);
        if (sysDictionaries != null && sysDictionaries.size() > 0) {
            return false;
        }

        return true;
    }
}
